package com.hzf.wxplatform.admin.controller.manage;

import com.hzf.common.util.HttpUtil;
import com.hzf.common.util.RedisUtil;
import com.hzf.common.util.WeixinUtil;
import com.hzf.common.util.WeixinUtils;
import com.hzf.wxplatform.dao.model.*;
import com.hzf.wxplatform.rpc.api.WxplatformWxmenuService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import io.swagger.annotations.ApiOperation;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

@Controller
public class WeiXinConfigController {
    @Autowired
    WxplatformWxmenuService wxplatformWxmenuService;
   /**
    * 后台首页
    * @return
    */
   @ApiOperation(value = "后台首页")
   @RequestMapping(value = "/wxMenu/cread", method = RequestMethod.GET)
   public String cread() throws  Exception {
       //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3c24e65abccc4f36&redirect_uri=http://hzf.nat100.top/wxOauth/getOpenid&response_type=code&scope=snsapi_userinfo&state=3#wechat_redirect
       Menu menu =new Menu();

       WxplatformWxmenu wxplatformWxmenu = new WxplatformWxmenu();
       WxplatformWxmenuExample wxplatformWxmenuExample = new WxplatformWxmenuExample();
       wxplatformWxmenuExample.createCriteria().andMenuIdIsNull();
       List<WxplatformWxmenu> rows =wxplatformWxmenuService.selectByExample(wxplatformWxmenuExample);
       for(WxplatformWxmenu wxplatformWxmenu1 :rows){
           Button button = new Button();
           button.setName(wxplatformWxmenu1.getName());
           button.setType(wxplatformWxmenu1.getType());
           button.setUrl(wxplatformWxmenu1.getUrl());
           WxplatformWxmenuExample wxplatformWxmenuExample1 = new WxplatformWxmenuExample();
           wxplatformWxmenuExample1.createCriteria().andMenuIdEqualTo(wxplatformWxmenu1.getId());
           List<WxplatformWxmenu> wxMenus =wxplatformWxmenuService.selectByExample(wxplatformWxmenuExample1);
           for(WxplatformWxmenu wxplatformWxmenu2:wxMenus){
               Button [] buttons = null;

               Button button1 = new Button();
               button1.setName(wxplatformWxmenu2.getName());
               button1.setType(wxplatformWxmenu2.getType());
               button1.setUrl(wxplatformWxmenu2.getUrl());
               buttons = new Button[]{button1};

               if(button.getSub_button()!=null){
                   Button[]  newStr = new Button[buttons.length+button.getSub_button().length];

                   for(int x=0;x<buttons.length;x++){
                       newStr[x] = buttons[x];
                   }
                   for(int y=0;y<button.getSub_button().length;y++){
                       newStr[buttons.length+y]=button.getSub_button()[y];
                   }
                   button.setSub_button(newStr);

               }else{
                   Button[]  newStr = new Button[buttons.length];
                   for(int x=0;x<buttons.length;x++){
                       newStr[x] = buttons[x];
                   }
                   button.setSub_button(newStr);

               }
           }
           Button [] buttons = null;
           buttons = new Button[]{button};
           if(menu.getButton()!=null){
               Button[]  newStr = new Button[buttons.length+menu.getButton().length];
               for(int x=0;x<buttons.length;x++){
                   newStr[x] = buttons[x];
               }
               for(int y=0;y<menu.getButton().length;y++){
                   newStr[buttons.length+y]=menu.getButton()[y];
               }
               menu.setButton(newStr);
           }
           else{
               Button[]  newStr = new Button[buttons.length];
               for(int x=0;x<buttons.length;x++){
                   newStr[x] = buttons[x];
               }
               menu.setButton(newStr);
             }
       }
       JSONObject  jsonObject = JSONObject.fromObject(menu);
       String ACCESS_TOKEN = RedisUtil.get("access_token");
       System.out.println(ACCESS_TOKEN);
       String outStr = jsonObject.toString();
       System.out.println(outStr);
       String url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+ACCESS_TOKEN;
       JSONObject  jo = WeixinUtil.doPostStr(url, outStr);

       System.out.println(jo.toString());
       System.out.println( jo.getInt("errcode") );
       if( jo.getInt("errcode")==0 ){
           System.out.println( "创建菜单成功" );
       }else{
           System.out.println("失败");
       }
     /*  System.out.println("执行了");
       JsonConfig jsonConfig = new JsonConfig();
       jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
       WxplatformWxmenu wxplatformWxmenu = new WxplatformWxmenu();
       WxplatformWxmenuExample wxplatformWxmenuExample = new WxplatformWxmenuExample();
       wxplatformWxmenuExample.createCriteria().andMenuIdIsNull();
       List<WxplatformWxmenu> rows =wxplatformWxmenuService.selectByExample(wxplatformWxmenuExample);
       for(WxplatformWxmenu wxplatformWxmenu1 :rows){
           WxplatformWxmenuExample wxplatformWxmenuExample1 = new WxplatformWxmenuExample();
           wxplatformWxmenuExample1.createCriteria().andMenuIdEqualTo(wxplatformWxmenu1.getId());
           List<WxplatformWxmenu> wxMenus =wxplatformWxmenuService.selectByExample(wxplatformWxmenuExample1);
           wxplatformWxmenu1.setWxplatformWxmenus(wxMenus);
       }
       WxMenu wxMenu = new WxMenu();
       wxMenu.setWxplatformWxmenus(rows);
      String s = JSONArray.fromObject(wxMenu,jsonConfig).toString();
       System.out.println(s);*/
       return "/manage/index.jsp";
   }




}
